package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {
    public static String pdfToString(InputStream inputStream){
        PDDocument document=null;
        try{
        document=PDDocument.load(inputStream);

            // 创建一个PDFTextStripper实例来提取文本
        PDFTextStripper pdfTextStripper=new PDFTextStripper();
        String text=pdfTextStripper.getText(document);
        return text;
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(document!=null){
                try{
                    document.close();
                    inputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(String[] args) throws FileNotFoundException {

        FileInputStream fileInputStream = new FileInputStream("E:/体检报告-刘爱国-男-69岁.pdf");

        String result = PDFUtil.pdfToString(fileInputStream);
        System.out.println(result);
    }
}
